<template>
  <div class="main">
    <div class="container">
        <div class="container-top">
            <div class="top">
                <div class="title">修改密码</div>
            </div>
            <div class="userinfo">
                <p class="account">账号：<el-input v-model="accountValue" class="accountInput"  disabled></el-input></p>
            </div>
        </div>
        <div class="content">
            <el-form ref="form" :model="form" label-width="100px" :rules="rules">
                <el-form-item label="旧密码">
                  <el-input v-model="form.oldpass" autocomplete="off" show-password class="common" placeholder="单行输入"></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="newpass">
                  <el-input v-model="form.newpass" autocomplete="off" show-password class="common" placeholder="单行输入"></el-input>
                </el-form-item>
                <el-form-item label="确定密码" prop="newpass2">
                  <el-input v-model="form.newpass2" autocomplete="off" show-password class="common" placeholder="单行输入"></el-input>
                </el-form-item>
                <el-form-item class="button">
                  <el-button icon="iconfont icon-guanbi" class="close" @click="back">取消</el-button>
                  <el-button type="primary" @click="onSubmit" icon="iconfont icon-baocun_mian" class="save">确认修改</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
  </div>
</template>
<script>
import { ModifyPass, UserInfo } from '@/api/login'
export default {
  name: 'Modify',
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.form.newpass2 !== '') {
          this.$refs.form.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.form.newpass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
        Showbg: false,
        accountValue: '',
        form: {
          oldpass: '',
          newpass: '',
          newpass2: ''
        },
        rules: {
            newpass: [
              { validator: validatePass, trigger: 'blur' }
            ],
            newpass2: [
              { validator: validatePass2, trigger: 'blur' }
            ],
        },
        token: ''
    }
  },
  methods: {
    // 修改密码
    queryModifyPass(){
        ModifyPass({
            token: this.token,
            pwdold: this.form.oldpass,
            pwd1: this.form.newpass,
            pwd2: this.form.newpass2
        }).then((result) => {
          if(result.code === 1){
            this.$message({
              message: '执行成功',
              type: 'success'
            })
            this.$router.go(-1);
            window.sessionStorage.clear();
          }
        })
    },
    // 获取用户信息
    queryUserInfo(){
      UserInfo({
        token: this.token
      }).then((result) => {
        const {data} = result;
        this.accountValue = data.user_name;
      })
    },
    onSubmit(){
      this.queryModifyPass();
    },
    back(){
      this.$router.go(-1)
    },
    backgroundShow(){
      this.$emit('backgroundShow',this.Showbg)
    }
  },
  created(){
    this.backgroundShow()
  },
  mounted(){
    const token = sessionStorage.getItem('token');
    this.token = token;
    this.queryUserInfo();
  }
}
</script>
<style lang='scss' scoped>
.main{
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    padding: 0;
    color: #242748;
    margin: 0 auto;
    position: relative;
    font-size: 18px;
    margin: 0 50px 20px 30px;
    text-align: left;
    font-family: "PingFang";
    .container{
        width: 100%;
        height: 925px;
        padding-bottom: 30px;
        background-color: #fff;
        border-radius: 10px;
        .container-top{
            .top{
                display: flex;
                margin-bottom: 30px;
                .title{
                    font-size: 26px;
                    font-family: "黑体";
                    color: #5673ff;
                    margin-right: 60px;
                }
            }
            .userinfo{
                display: flex;
                align-items: center;
                .account{
                    display: flex;
                    align-items: center;
                    font-weight: 700;
                    .accountInput{
                        width: 70%;
                        height: 32px;
                        line-height: 32px;
                    }
                    /deep/.el-input__inner{
                        height: 100%;
                        line-height: 0;
                        color: #5673ff;
                        font-size: 15px;
                        letter-spacing: 1px;
                        background-color: #f8f9fe;
                        border: solid 1px #f1f2f4;
                    }
                }
                .modify-pass{
                  color: #5673ff;
                  font-size: 16px;
                }
            }
        }
        .container-top{
            padding: 33px 45px 25px;
            border-bottom: solid 1px #e7e9eb;
        }
        .content{
            padding: 40px;
            .common{
                width: calc(100% - 40%);
            }
            /deep/.el-input__inner{
              font-size: 18px;
              color: #9d9fa2;
              font-family: "PingFang";
            }
            /deep/.el-input.is-disabled .el-input__inner{
              background-color: #f4f7fc;
            }
            /deep/.el-form-item{
              margin-bottom: 38px;
            }
            /deep/.el-form-item__label{
                text-align: left;
                color: #242748;
                font-size: 18px;
                font-weight: 700;
            }
            /deep/.button .el-form-item__content{
                margin-left: 0!important;
            }
            /deep/.el-upload{
                float: left;
            }
            /deep/.el-upload__tip{
              font-size: 18px!important;
              color: #242748!important;
            }
            /deep/ul.el-upload-list.el-upload-list--picture{
                width: 40%;
            }
            /deep/.el-upload-list--picture .el-upload-list__item{
                height: 70px;
                line-height: 70px;
                background-color: #f4f7fc;
                border: none;
            }
            /deep/.el-upload-list--picture .el-upload-list__item-thumbnail{
                width: 50px;
                height: 50px;
                margin-left: -70px;
            }
            /deep/.el-upload-list--picture .el-upload-list__item.is-success .el-upload-list__item-name{
                line-height: 48px;
                color: #242748;
                font-size: 14px;
                font-weight: 700;
            }
            .uploadBtn{
              padding: 7px 12px 7px 10px;
              margin-right: 10px;
              background-color: #e5eefe;
              border: solid 1px #c8e3fe;
              color: #2197fd;
              font-size: 14px;
              letter-spacing: 1px;
            }
            /deep/.el-icon-circle-plus{
              color: #2197fd;
              font-size: 16px;
            }
            .preview{
              display: flex;
              align-items: center;
              width: 36%;
              margin-top: 20px;
              padding: 20px 22px;
              background-color: #f4f7fc;
              border-radius: 5px;
              .img{
                width: 30px;
                height: 30px;
                vertical-align: middle;
                img{
                  border-radius: 2px;
                }
              }
              .details{
                width: 70%;
                margin: 0 0 0 20px;
                .title{
                  width: 90%;
                  overflow: hidden;
                  text-overflow: ellipsis;
                  white-space: nowrap;
                  color: #242748;
                  font-size: 14px;
                  font-weight: 700;
                }
                p{
                  line-height: 22px;
                  font-size: 12px;
                  color: #a1a4b5;
                  span{
                    margin-right: 10px;
                  }
                }
              }
              .preview-btn{
                height: 30px;
                &>div{
                  float: left;
                  width: 18px;
                  height: 18px;
                  margin-left: 18px;
                }
                .del{
                  width: 16px;
                }
              }
            }
            .save{
              background-color: #5673ff;
              border: solid 1px #2d4eff;
              box-shadow: 0px 4px 15.52px 0.48px rgba(73, 100, 245, 0.39);
            }
            .close{
                border: solid 1px #e7e9eb;
                background-color: rgb(255, 255, 255);
                box-shadow: 0px 4px 15.52px 0.48px rgba(73, 100, 245, 0.14);
                color: #242748;
            }
            .save,.close{
                padding: 8px 15px 8px 20px;
                font-size: 14px;
                font-family: "PingFang";
                letter-spacing: 1px;
                margin-right: 10px;
                /deep/i{
                margin-right: 8px;
                font-size: 18px;
                vertical-align: middle;
              }
            }
        }
    }
}
</style>